<template>
	<view class="subject-list">
		<view class="template-item" v-for="(item, index) in applyList" :key="index" @click="handleSelected(item)">
			<view class="template-item-top" :style="{'background-image': 'url(' + backgroundList[index % 6] + ')'}">
				<view class="template-item-name">
					<text class="template-item-name-text">{{item.name}}</text>
					<text class="template-item-date-text">集训日期：{{item.beginTime}}-{{item.endTime}}</text>
					<text class="template-item-apply-date-text">报名截止日期：{{item.deadline}}</text>
				</view>
			</view>
			<view class="template-item-bottom">
				<text class="template-item-desc">{{item.tips}}</text>
			</view>
		</view>

		<view class="no-data-tip" v-if="applyList.length == 0">
			<image class="no-data-icon" src="../../static/no-data.png" mode="widthFix"></image>
			<text class="no-data-text">暂无报名，敬请期待！</text>
		</view>

		<view class="height"></view>
	</view>
</template>

<script>
	import apiRqs from "@/api/api-request.js";
	export default {
		data() {
			return {
				type: "",
				userOpenid: "",
				applyList: [],
				backgroundList: ['https://biapi.vaubang.cn/UploadFiles/Images/online-bg-1.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-2.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-3.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-4.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-5.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-6.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-7.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-8.png',
					'https://biapi.vaubang.cn/UploadFiles/Images/online-bg-9.png'
				],
			}
		},
		onShow() {
			this.handleUserOpenid();
		},
		onLoad() {
			getApp().showLoading();
			this.handleUserOpenid();
		},
		methods: {
			// 获取用户openid
			handleUserOpenid() {
				var that = this;
				uni.login({
					provider: "weixin",
					success(loginRes) {
						let code = loginRes.code;
						uni.request({
							url: "https://bydapi.vaubang.cn/api/v1/WxOpen/FindOpenId?code=" +
								code,
							method: "post",
							header: {
								"content-type": "application/json;charset=utf-8;"
							},
							success(res) {
								console.log('用户openid', res);
								if (res.data.code == 200) {
									that.userOpenid = res.data.data.openid;
									that.handleApplyList();
								} else {
									getApp().showToast(res.data.msg);
								}
							},
							fail(err) {
								console.log(err);
							}
						})
					}
				})
			},
			// 获取报名列表
			handleApplyList() {
				var that = this;
				let rqs = {
					"pageSize": 9999,
					"pageIndex": 1,
					"orderBy": "asc"
				};
				apiRqs._getApplyList(rqs).then((res) => {
					uni.hideLoading();
					console.log('专题专项报名列表', res);
					if (res.code == 200) {
						let applyData = JSON.parse(JSON.stringify(res.data.data));
						let newList = [];
						for (let i = 0; i < applyData.length; i++) {
							if (applyData[i].type == '专题专项') {
								applyData[i].endTime = applyData[i].endTime.substring(applyData[i].endTime.length -
									8, applyData[i].endTime.length);
								newList.push(applyData[i]);
							}
						}
						that.applyList = newList;
					} else {
						getApp().showToast(res.msg);
					}
				}).catch((err) => {
					console.log('err', err);
				})
			},
			handleSelected(item) {
				uni.setStorageSync("subjectInfo", item);
				uni.navigateTo({
					url: "/packageA/workbench/subject-list-detail?openid=" + this.userOpenid
				})
			},
			onPullDownRefresh() {
				this.handleUserOpenid();
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 1500);
			},
			onShareAppMessage() {
				return this.getShareData();
			},
			// 分享给好友
			getShareData() {
				let title = "邀请您参加专题/专项线上集训";
				let imageUrl = "https://biapi.vaubang.cn/UploadFiles/Images/subject-cover.png";
				let path = '/packageA/workbench/subject-list';
				return {
					title,
					imageUrl,
					path,
				};
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f5f5f5;
	}

	.subject-list {
		.height {
			height: 40px;
		}

		.no-data-tip {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 200rpx;

			.no-data-icon {
				width: 280rpx;
			}

			.no-data-text {
				font-size: 24rpx;
				color: #999;
				margin-top: 20rpx;
			}
		}

		.template-item {
			margin: 10px 15px;
			border-radius: 3px;
			position: relative;

			.selected-icon {
				position: absolute;
				bottom: 0;
				right: 0;
				font-size: 18px;
				color: #fff;
				background-color: #008aff;
				border-radius: 2px;
			}

			&.active {
				border: 1px solid #008aff;
			}

			.template-item-top {
				width: 100%;
				height: 120px;

				border-top-right-radius: 3px;
				border-top-left-radius: 3px;
				background-repeat: no-repeat;
				background-size: cover;
				position: relative;

				.template-item-name {
					background: rgba(0, 0, 0, 0.3);
					width: 100%;
					height: 120px;
					position: absolute;
					top: 0;
					left: 0;
					text-align: center;
					display: flex;
					flex-direction: column;
					justify-content: center;

					.template-item-name-text,
					.template-item-date-text,
					.template-item-apply-date-text {
						font-size: 18px;
						color: #fff;
						padding: 0px 20px;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						width: 100%;
						font-weight: bold;
						box-sizing: border-box;
					}

					.template-item-date-text {
						font-size: 13px;
						margin-top: 5px;
					}

					.template-item-apply-date-text {
						font-size: 12px;
						margin-top: 5px;
					}
				}
			}

			.template-item-bottom {
				background: #fff;
				padding: 5px 15px 10px 15px;
				border-bottom-left-radius: 3px;
				border-bottom-right-radius: 3px;
				text-align: center;

				.template-item-desc {
					font-size: 12px;
					color: #333;
					font-weight: 500;
				}
			}
		}

		.no-template-tips {
			margin-top: 50px;
			display: flex;
			flex-direction: column;
			align-items: center;

			.tips-icon {
				font-size: 50px;
				color: #999999;
			}

			.tips-text {
				font-size: 14px;
				color: #999999;
				margin-top: 10px;
			}
		}
	}
</style>
